# （1）升级 /安装 cmake 至 ≥ 3.27

# 安装依赖
apt-get update
apt-get install -y wget tar

# 下载 CMake 的源码 /预编译包或官方安装包
cd /usr/local
wget https://github.com/Kitware/CMake/releases/download/v3.27.0/cmake-3.27.0-linux-x86_64.tar.gz
tar zxvf cmake-3.27.0-linux-x86_64.tar.gz
mv cmake-3.27.0-linux-x86_64 cmake-3.27

# 把新的 cmake 放到 PATH 前面
ln -s /usr/local/cmake-3.27/bin/cmake /usr/local/bin/cmake
ln -s /usr/local/cmake-3.27/bin/ctest /usr/local/bin/ctest
ln -s /usr/local/cmake-3.27/bin/ccmake /usr/local/bin/ccmake
ln -s /usr/local/cmake-3.27/bin/cpack /usr/local/bin/cpack

# 验证 cmake 版本
cmake --version

# （2）安装 typing_extensions 包到 python3.10 环境

python3.10 -m pip install typing_extensions -i https://pypi.tuna.tsinghua.edu.cn/simple

# （3）再尝试编译 PyTorch

cd /root/pytorch
git submodule sync
git submodule update --init --recursive

export TORCH_CUDA_ARCH_LIST="120;100;90;86;80;75;70;60"
export CMAKE_PREFIX_PATH="$(python3.10 -c 'import sys; print(sys.prefix)')"

python3.10 setup.py install

# （4）验证编译结果

python3.10 - << 'EOF'
import torch
print("Torch version:", torch.__version__)
print("CUDA version:", torch.version.cuda)
print("CUDA available:", torch.cuda.is_available())
print("Supported arch list:", torch.cuda.get_arch_list())
EOF
